package com.lagou.controller;

import com.lagou.entity.OrderDetail;
import com.lagou.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderRepository orderRepository;

    @GetMapping("index")
    public String index(ModelMap modelMap) {
        modelMap.put("title", "商品列表");
        final List<OrderDetail> list = orderRepository.findAll();
        log.info("list:{}", list);
        modelMap.put("list", list);
        return "orderlist";
    }
    @GetMapping("/{id}")
    public String detail(@PathVariable Integer id, ModelMap modelMap) {
        final Optional<OrderDetail> byId = orderRepository.findById(id);
        final OrderDetail orderDetail = byId.get();
        modelMap.put("detail", orderDetail);
        return "orderdetail";
    }

    @PostMapping("/pay")
    @ResponseBody
    public Map<String,Object> pay(@RequestParam Map<String,Object> paramMap) throws InterruptedException {
        log.info("paramMap:" + paramMap);
        Map<String,Object> resultMap = new HashMap<>();

        String id = (String)paramMap.get("id");
        if(Objects.equals(id, "1")) {
            log.info("正常流程。。。。。。。。");
        }if (Objects.equals(id, "2")){
            log.info("超时流程。。。。。。。。");
            Thread.sleep(10 * 1000);
        }
        resultMap.put("status", 200);
        resultMap.put("msg", "支付成功");
        return resultMap;
    }

    @RequestMapping("/go")
    public String distributeGo() {
        return "任务已经下发，等待送单。。。";
    }

    @RequestMapping("/notgo")
    public String getAccumulatedTask() {
        return "notgo";
    }


}
